<?php
    session_start();
   	require_once 'config.php';
	require_once 'Date.php';

	$rs=getQuestion();

	echo $var;

	if( isset($_REQUEST["username"]) && isset($_REQUEST["password"]) && isset($_REQUEST["que_id"])  && isset($_REQUEST["secans"] ))
    {
        $userexts=checkUserphp($_REQUEST["username"],md5($_REQUEST["password"]));

        if ($userexts== false)
        {
              addUser($_REQUEST["username"],$_REQUEST["password"],$_REQUEST["que_id"],$_REQUEST["secans"]);
              $_SESSION['logged'] = true;
              $_SESSION['username'] = $_POST['username'];
              header('Location:table.php');
              exit;
         }
         else
            echo "User already exists! Select different username!";
    }

	getCreditCards($user);

?>
<html>
<head>
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" >

		function validateForm() {
			if(document.frmLogin.username.value == '') {
				alert("Please enter a valid username.");
				document.frmLogin.username.focus();
				return false;
			}
			else if(document.frmLogin.password.value == '') {
				alert("Please enter a valid password.");
				document.frmLogin.password.focus();
				return false;
			}
			else if(document.frmLogin.password.value != document.frmLogin.passwordAgain.value) {
				alert("The passwords do not match.");
				document.frmLogin.password.focus();
				return false;
			}
			else if(document.frmLogin.secans.value == '') {
				alert("Please enter an answer to the secret question.\nThis will allow you to recover your password in case you forget it.");
				document.frmLogin.secans.focus();
				return false;
			}
			document.frmLogin.submit();
		}

    function checkname()
    {
    var xmlHttp;
    var username = document.frmLogin.username.value;
	xmlHttp = new XMLHttpRequest();

	xmlHttp.onreadystatechange  = function()
	{
		if(xmlHttp.readyState == 4) {
//			alert(xmlHttp.responseText);
			var resultHolder = document.getElementById("checkresult");
			if(xmlHttp.responseText == "unavailable") {
				resultHolder.innerHTML = "<font color=\"red\">Username is not available</font>";
				document.getElementById('btnRegister').disabled = true;

			}
			else {
				resultHolder.innerHTML = "<font color=\"green\">Userame is available!</font>";
				document.getElementById('btnRegister').disabled = false;
			}
    	}
    }

	xmlHttp.open("GET","checkuser.php?username="+username,true);
    xmlHttp.send(null);

	}
    </script>
    <title>Register</title>
</head>
<body>
<form action="register.php" name="frmLogin" method="post">
<br/><br/><br/>
<table cellspacing="2" cellpadding="3" align="center" class="menu_table">
	<tr>
<td colspan="2"><b>New User Registration</b></td>
</tr>
    <tr>
        <td>Username:</td><td><input type='text' name='username'>&nbsp;<span style="color: red;">*</span>&nbsp;<span id="checkresult"></span></td>
        </tr>
    <tr>
        <td>Password:</td><td> <input type='password' name='password' onfocus="checkname();" > <span style="color: red;">*</span></td>
    </tr>    
    <tr>
        <td>Confirm Password:</td><td> <input type='password' name='passwordAgain'> <span style="color: red;">*</span></td>
    </tr>
    <tr>
        <td>Secret Question:</td>
        <td><select name="que_id" id="idquestion">
           <? foreach($rs as $row) { ?>

             <option value="<?=$row["que_id"]?>"><?=$row["ques_des"]?></option>
            <? } ?>
            </select> <span style="color: red;">*</span></td>
    </tr>
    <tr>
        <td>Secret Answer:</td><td> <input type='text' name='secans'> <span style="color: red;">*</span></td>
    </tr>
    <tr>
        <td colspan="2" style="text-align: center;"><input type="button" value="Register" name="register" id="btnRegister" onclick="validateForm();"></td>
    </tr>
</table>
</form>
</body>
</html>

